package cn.qp.service.impl;

import cn.qp.service.AsyncService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * @author qipan
 * @date 2020/01/11 11:57
 */
@Service
public class AsyncServiceImpl implements AsyncService {
    private static Random random = new Random();
    @Override
    @Async
    public Future<String> doTask1() throws InterruptedException {
        System.out.println("Task 1 start!");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("Task 1 end!");
        System.out.println("Task 1 cost time: " + (end - start) + "ms");
        return new AsyncResult<>("Task 1 over!");
    }

    @Override
    @Async
    public Future<String> doTask2() throws InterruptedException {
        System.out.println("Task 2 start!");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("Task 2 end!");
        System.out.println("Task 2 cost time: " + (end - start) + "ms");
        return new AsyncResult<>("Task 2 over!");
    }

    @Override
    @Async
    public Future<String> doTask3() throws InterruptedException {
        System.out.println("Task 3 start!");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));
        long end = System.currentTimeMillis();
        System.out.println("Task 3 end!");
        System.out.println("Task 3 cost time: " + (end - start) + "ms");
        return new AsyncResult<>("Task 3 over!");
    }
}
